cmake_minimum_required(VERSION 3.16)

#set(MCU_TOOLCHAIN_PATH /config/arm/gcc/)
#set(MCU_TARGET_TRIPLET arm-none-eabi)
#set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../cmake/mcu_gcc.cmake)

set(MCU_TYPE "N32G43x")
project(RT_Thread20_HID_Mouse C ASM)

INCLUDE_DIRECTORIES("inc")
INCLUDE_DIRECTORIES("../../../../../firmware/CMSIS/core")
INCLUDE_DIRECTORIES("../../../../../firmware/CMSIS/device")
INCLUDE_DIRECTORIES("../../../../../firmware/n32g43x_std_periph_driver/inc")
#INCLUDE_DIRECTORIES("../../../../../firmware/n32g43x_usbfs_driver/inc")

INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/include")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/include/libc")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/libc/compilers/newlib")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/libc/compilers/minilibc")

INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/drivers/include/drivers")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/drivers/include/ipc")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/drivers/include")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/finsh")

INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/drivers/cherryusb/common")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/drivers/cherryusb/core")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/drivers/cherryusb/class/hid")
INCLUDE_DIRECTORIES("../../../../../middlewares/rt-thread/components/drivers/cherryusb/port/fsdev")

INCLUDE_DIRECTORIES("../DeviceDrivers/gpio/inc")
INCLUDE_DIRECTORIES("../DeviceDrivers/uart/inc")

AUX_SOURCE_DIRECTORY("src" DIR_SRCS)
AUX_SOURCE_DIRECTORY("../DeviceDrivers/gpio/src" DIR_SRCS)
AUX_SOURCE_DIRECTORY("../DeviceDrivers/uart/src" DIR_SRCS)
AUX_SOURCE_DIRECTORY("../../../../../firmware/CMSIS/device" DIR_SRCS)

#AUX_SOURCE_DIRECTORY("../../../../../firmware/n32g43x_usbfs_driver/src" DIR_SRCS)

AUX_SOURCE_DIRECTORY("../../../../../middlewares/rt-thread/src" DIR_SRCS)
AUX_SOURCE_DIRECTORY("../../../../../middlewares/rt-thread/libcpu/cortex-m4" DIR_SRCS)

AUX_SOURCE_DIRECTORY("../../../../../middlewares/rt-thread/components/drivers/src" DIR_SRCS)
AUX_SOURCE_DIRECTORY("../../../../../middlewares/rt-thread/components/drivers/serial" DIR_SRCS)

AUX_SOURCE_DIRECTORY("../../../../../middlewares/rt-thread/components/libc/compilers/minilibc" DIR_SRCS)

set(STARTUP_ASM ../../../../../firmware/CMSIS/device/startup/startup_n32g43x_gcc_rtt.s)

set(RTTHREAD_ASM ../../middlewares/rt-thread/libcpu/cortex-m4/context_gcc_w.s)

set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../firmware/CMSIS/device/n32g43x_flash_rtt.ld)

add_definitions("-DUSE_STDPERIPH_DRIVER")
add_definitions("-DN32G43X")
add_definitions("-DSYSCLK_FREQ=96000000")
add_definitions("-DRT_USING_MINILIBC")

set(PROJECT_SOURCES
    ${DIR_SRCS}
    ${STARTUP_ASM}
    "../../../../../firmware/n32g43x_std_periph_driver/src/n32g43x_gpio.c"
    "../../../../../firmware/n32g43x_std_periph_driver/src/n32g43x_rcc.c"
    "../../../../../firmware/n32g43x_std_periph_driver/src/misc.c"
    "../../../../../firmware/n32g43x_std_periph_driver/src/n32g43x_dma.c"
    "../../../../../firmware/n32g43x_std_periph_driver/src/n32g43x_exti.c"
    "../../../../../firmware/n32g43x_std_periph_driver/src/n32g43x_flash.c"
    "../../../../../firmware/n32g43x_std_periph_driver/src/n32g43x_usart.c"
    "../../../../../firmware/n32g43x_std_periph_driver/src/n32g43x_pwr.c"
    ${RTTHREAD_ASM}
    "../../../../../middlewares/rt-thread/components/drivers/misc/pin.c"
    "../../../../../middlewares/rt-thread/components/drivers/cherryusb/class/hid/usbd_hid.c"
    "../../../../../middlewares/rt-thread/components/drivers/cherryusb/core/usbd_core.c"
    "../../../../../middlewares/rt-thread/components/drivers/cherryusb/port/fsdev/usb_dc_fsdev.c"
)

message("#########################################")
message( "DEVICE: ${MCU_TYPE}" )
message("#########################################")


add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} )
target_link_libraries(${PROJECT_NAME} MCU::NoSys ${MCU_TYPE})

target_compile_options(${PROJECT_NAME} PUBLIC "-Os")
mcu_add_linker_script(${PROJECT_NAME} PUBLIC ${LINKER_SCRIPT})


target_link_options(${PROJECT_NAME} PUBLIC -Xlinker -Map=${PROJECT_NAME}.map)

mcu_generate_binary_file(${PROJECT_NAME})
mcu_print_size_of_target(${PROJECT_NAME})

